// pages/find/vision/detail/detail.js
import { HTTP } from '../../../../utils/http.js'
let ajax = new HTTP()
var app = getApp()

Page({
    data: {
        detailTitle: "",
        detailId: "",
        posterSrc: "",
        videoSrc: "",
        isLove: 0,
        isLike: 0,
        likeNum: 0,
        loveNum: 0,
        title: "",
        content: "",
        objFit: "fill"
    },
    onBindPlay() {
        this.setData({
            objFit: "contain"
        })
    },
    onLoveTap (event) {
        let isLove
        let loveNum = this.data.loveNum
        if (this.data.isLove === 1) {
            // 取消收藏        
            isLove = 0
            loveNum--
        } else {
            // 收藏
            isLove = 1
            loveNum++
        }
        wx.showToast({
            title: isLove === 1 ? '收藏成功' : '取消成功',
            icon: "success",
            duration: 1500
        })
        this._getLoveInfo(this.data.detailId, app.globalData.memberid)
        this.setData({
            isLove: isLove,
            loveNum: loveNum
        })
    },
    onLikeTap (event) {
        let isLike
        let likeNum = this.data.likeNum
        if (this.data.isLike === 1) {
            // 取消点赞        
            isLike = 0
            likeNum--
        } else {
            // 点赞
            isLike = 1
            likeNum++
        }
        wx.showToast({
            title: isLike === 1 ? '点赞成功' : '取消成功',
            icon: "success",
            duration: 1500
        })
        this._getLikeInfo(this.data.detailId, app.globalData.memberid)
        this.setData({
            isLike: isLike,
            likeNum: likeNum
        })
    },
    onLoad (options) {
        this.data.detailTitle = options.title
        this.data.detailId = options.id
        wx.setNavigationBarTitle({
            title: this.data.detailTitle
        })
        this._getDetailList(app.globalData.memberid, options.id)
    },
    // 转发 
    onShareAppMessage: function () {

    },
    // 获取信息
    _getDetailList(memberid, id) {
        ajax.request({
            url: "videolist/read",
            data: {
                memberid: memberid,
                id: id
            },
            success: (res) => {
                res = res.data
                console.log(res)
                this.setData({
                    posterSrc: res.pic,
                    title: res.title,
                    content: res.content,
                    videoSrc: res.videourl,
                    isLike: res.hasprize,
                    likeNum: res.prizenum,
                    isLove: res.hascoll,
                    loveNum: res.collectnum
                })
            }
        }) 
    },
    // 请求收藏
    _getLoveInfo(id, memberid) {
        ajax.request({
            url: "collectlist",
            method: "POST",
            data: {
                reltype: 4,
                relid: id,
                memberid: memberid
            },
            success: (res) => {
                console.log(res)
            }
        })
    },
    // 请求点赞
    _getLikeInfo(id, memberid) {
        ajax.request({
            url: "collectlist",
            method: "POST",
            data: {
                reltype: 5,
                relid: id,
                memberid: memberid
            },
            success: (res) => {
                console.log(res)
            }
        })
    }
})